package com.moyu.user.service.impl;

import com.moyu.user.domain.permission.Permission;
import com.moyu.user.domain.permission.dto.PermissionRoleDTO;
import com.moyu.user.mapper.PermissionMapper;
import com.moyu.user.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 赵彦博
 * @Date 2024/11/20 8:46
 */
@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Permission queryById(Long id) {
        return permissionMapper.selectById(id);
    }

    @Override
    public List<Permission> list(boolean treeMode) {
        List<Permission> permissions = permissionMapper.list();
        if (treeMode) {
            return convertListToTree(permissions);
        }
        return permissions;
    }

    @Override
    public List<Permission> listByUsername(String username, boolean treeMode) {
        List<Permission> permissions = permissionMapper.listByUsername(username);
        if (treeMode) {
            return convertListToTree(permissions);
        }
        return permissions;
    }

    @Override
    public List<Permission> listByRoleId(Long roleId, boolean treeMode) {
        List<Permission> permissions = permissionMapper.listByRoleId(roleId);
        if (treeMode) {
            return convertListToTree(permissions);
        }
        return permissions;
    }

    @Override
    public List<PermissionRoleDTO> listRoleByType(Permission.Type type) {
        return permissionMapper.listRoleByType(type);
    }

    @Override
    public Permission create(Permission permission) {
        permissionMapper.insert(permission);
        return permission;
    }

    @Override
    public Permission update(Permission permission) {
        permission.setUpdateTime(LocalDateTime.now());
        permissionMapper.update(permission);
        return queryById(permission.getId());
    }

    @Override
    public boolean delete(Long id) {
        return permissionMapper.delete(id) > 0;
    }

    @Override
    public List<Permission> convertListToTree(List<Permission> permissions) {
        Map<Long, Permission> map = new HashMap<>();
        List<Permission> result = new ArrayList<>();
        for (Permission permission : permissions) {
            map.put(permission.getId(), permission);
        }
        for (Permission current : permissions) {
            int parentId = current.getParentId();
            if (parentId == 0) {
                result.add(current);
            } else {
                Permission parent = map.get(parentId);
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(current);
                }
            }
        }
        return result;
    }

    @Override
    public List<Permission> convertTreeToList(List<Permission> permissions) {
        List<Permission> result = new ArrayList<>();
        for (Permission permission : permissions) {
            List<Permission> children = permission.getChildren();
            if (children != null && children.size() > 0) {
                for (Permission second : children) {
                    result.add(second);
                    List<Permission> third = second.getChildren();
                    if (third != null && third.size() > 0) {
                        result.addAll(third);
                    }
                }
            }
        }
        return result;
    }
}
